package com.hqyj.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hqyj.dao.MenuMapper;
import com.hqyj.entity.Menu;
import com.hqyj.entity.ResultVo;
import com.hqyj.service.IMenuService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.Map;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author rock
 * @since 2022-09-20
 */
@Service
public class MenuServiceImpl extends ServiceImpl<MenuMapper, Menu> implements IMenuService {

    @Resource
    MenuMapper menuMapper;

    /**
     * 分页查询,以及dishName的模糊查询
     * @param menu
     * @return
     */
    @Override
    public ResultVo queryMenuByPage(Menu menu) {
        ResultVo vo = new ResultVo();
        //创建分页对象
        Page<Menu> page = new Page<>(menu.getPage(), menu.getRow());
        //查询分页
        QueryWrapper<Menu> wrapper = new QueryWrapper<>();
        Page<Menu> menuPage = null;
        wrapper.orderByAsc("did");
        if (menu.getDishName() != null && menu.getDishName() != "") {
            //去除前后以及中间的空格
            String dishName = menu.getDishName().trim();
            char[] chars = dishName.toCharArray();
            for (char aChar : chars) {
                if (aChar == ' ') {
                    continue;
                } else {
                    System.err.println("aChar:" + aChar);
                    wrapper.like("dish_name", aChar);
                    menuPage = menuMapper.selectPage(page, wrapper);
                }
            }
        } else {
            //当不传dishName值时(dishName=null),也可查询
            menuPage = menuMapper.selectPage(page, wrapper);
        }
        if (menuPage.getRecords().size() > 0) {
            //记录分页数据
            Map<String, Object> map = new HashMap<>();
            //当前页集合
            map.put("list", menuPage.getRecords());
            //总条数
            map.put("total", menuPage.getTotal());
            //总页数
            map.put("pageTotal", menuPage.getPages());
            vo.setData(map);
            vo.setSuccess(true);
            vo.setInfo("菜品查询成功");
        } else {
            vo.setInfo("菜品查询失败");
        }
        return vo;
    }

    /**
     * 菜品添加
     * @param menu
     * @return
     */
    @Override
    @Transactional
    public ResultVo addMenu(Menu menu) {
        ResultVo vo = new ResultVo();
        //查重（通过菜品名查询,排除出现同菜品名）
        QueryWrapper<Menu> wrapper = new QueryWrapper<>();
        wrapper.eq("dish_name", menu.getDishName());
        Menu one = menuMapper.selectOne(wrapper);
        if (one != null) {
            vo.setInfo("菜名/菜品编号重复,请重新添加");
            return vo;
        }
        int num = menuMapper.insert(menu);
        if (num > 0) {
            vo.setSuccess(true);
            vo.setInfo("菜品添加成功");
        } else {
            vo.setInfo("菜品添加失败");
        }
        return vo;
    }

    /**
     * 修改菜品信息
     * @param menu
     * @return
     */
    @Override
    @Transactional
    public ResultVo editMenu(Menu menu) {
        ResultVo vo = new ResultVo();
        LambdaUpdateWrapper<Menu> wrapper = new LambdaUpdateWrapper<>();
        wrapper.eq(Menu::getMid, menu.getMid());
        int num = menuMapper.update(menu, wrapper);
        if (num > 0) {
            vo.setInfo("修改成功");
            vo.setSuccess(true);
        }else {
            vo.setInfo("修改失败");
        }
        return vo;
    }

    /**
     * 通过did(菜品编号)删除菜品
     * @param did
     * @return
     */
    @Override
    @Transactional
    public ResultVo delMenuByDid(Integer did) {
        ResultVo vo = new ResultVo();
        UpdateWrapper<Menu> wrapper = new UpdateWrapper<>();
        wrapper.eq("did", did);
        int num = menuMapper.delete(wrapper);
        if (num > 0) {
            vo.setInfo("删除成功");
            vo.setSuccess(true);
        } else {
            vo.setInfo("删除失败");
        }
        return vo;
    }

    /**
     * 修改菜品状态(传值did,state)
     * @param menu
     * @return
     */
    @Override
    @Transactional
    public ResultVo editStateByDid(Menu menu) {
        ResultVo vo = new ResultVo();
        LambdaUpdateWrapper<Menu> wrapper = new LambdaUpdateWrapper<>();
        wrapper.eq(Menu::getDid,menu.getDid());
        int num = menuMapper.update(menu, wrapper);
        if (num > 0) {
            vo.setInfo("状态修改成功");
            vo.setSuccess(true);
        }else {
            vo.setInfo("状态修改失败");
        }
        return vo;
    }
}
